from django.http import JsonResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

from Completion.models import Completion
from ShortAnswer.models import ShortAnswer
from service.models import Class, personalQB, Student, personalReport

RIGHT = 1
WRONG = 0
NO_ANSWER = -1

CHOICE = '1'
COMPLETION = '2'
SHORT_ANSWER = '3'


@csrf_exempt
def studentSubmit(request):  # 获取学生提交的信息
    TypeID = request.POST.get('TypeID')  # 题目类型
    Account = request.session.get('account')  # 学生账号
    questionID = request.POST.getlist('ID', '')  # 问题ID，list
    submitAnswer = request.POST.getlist('submitAnswer', '')  # 学生提交的答案 , list
    if TypeID == '1':  # 选择
        recordData(Account, TypeID, questionID, submitAnswer)
    elif TypeID == '2':  # 填空
        recordData(Account, TypeID, Completion, questionID, submitAnswer)
        return render(request, 'stu_completion_wrong.html')
    elif TypeID == '3':  # 简答
        recordData(Account, TypeID, ShortAnswer, questionID, submitAnswer)
        return render(request, 'stu_shortAnswer_wrong.html')


@csrf_exempt
def recordData(Account, TypeID, questionType, questionID, submitAnswer):  # 录入到personalQB, personalReport
    """
    @param Account:学生的id
    @param TypeID:题型ID，定义在表单Name上
    @param questionType:问题类型，与题型ID相关，1.选择，2.填空，3.简答
    @param questionID:每种题型的唯一标识符，
    @param submitAnswer:学生提交上来的answer
    """
    for singleID, singleSubmitAnswer in zip(questionID, submitAnswer):
        selStu = personalReport.objects.get(stu_account=Account)
        questionRight = questionType.objects.filter(ID=singleID).values()[0]  # 找到对应问题ID的那一条问题
        test = personalQB.objects.filter(Account=Account).values().count()  # 查看条数
        status = ''
        personal = personalQB()
        if singleSubmitAnswer == questionRight.get('answer'):  # 学生答对了
            status = RIGHT
            personal = personalQB(TypeID=TypeID, questionID=singleID, statusID=status, Account=Account,
                                  rightAnswer=questionRight.get('answer'), wrongAnswer='你答对了！',
                                  question=questionRight.get('question'))
        elif singleSubmitAnswer == '':
            status = NO_ANSWER
            personal = personalQB(TypeID=TypeID, questionID=singleID, statusID=status, Account=Account,
                                  rightAnswer=questionRight.get('answer'), wrongAnswer='你没有回答这个问题！',
                                  question=questionRight.get('question'))
        elif singleSubmitAnswer != questionRight.get('answer'):
            status = WRONG
            personal = personalQB(TypeID=TypeID, questionID=singleID, statusID=status, Account=Account,
                                  rightAnswer=questionRight.get('answer'), wrongAnswer=singleSubmitAnswer,
                                  question=questionRight.get('question'))
        recordToPersonalReport(selStu, TypeID, singleID, status)
        recordToClass(Account, TypeID, singleID, status)
        if not personalQB.objects.filter(Account=Account, questionID=singleID, TypeID=TypeID):
            personal.save()


@csrf_exempt
def recordToPersonalReport(selStu, TypeID, questionID, statusID):  # 保存到personalReport
    if TypeID == CHOICE:
        pass
    elif TypeID == COMPLETION:  # 填空
        if statusID == RIGHT:
            if personalQB.objects.filter(TypeID=TypeID, questionID=questionID):
                return
            else:
                selStu.completionRight = selStu.completionRight + 1
        else:
            if personalQB.objects.filter(TypeID=TypeID, questionID=questionID):
                return
            else:
                selStu.completionWrong = selStu.completionWrong + 1
    elif TypeID == SHORT_ANSWER:  # 简答
        if statusID == RIGHT:
            if personalQB.objects.filter(TypeID=TypeID, questionID=questionID):
                return
            else:
                selStu.shortAnswerRight = selStu.shortAnswerRight + 1
        else:
            if personalQB.objects.filter(TypeID=TypeID, questionID=questionID):
                return
            else:
                selStu.shortAnswerWrong = selStu.shortAnswerWrong + 1
    selStu.save()


@csrf_exempt
def recordToClass(Account, TypeID, questionID, statusID):
    if Student.objects.get(Account=Account).Class is None:
        return
    ClassId = Student.objects.get(Account=Account).Class
    stuClass = Class.objects.get(tch_account=ClassId)
    if TypeID == CHOICE:
        pass
    elif TypeID == COMPLETION:  # 填空
        if statusID == RIGHT:
            stuClass.totalCompletionRight = stuClass.totalCompletionRight + 1
        else:
            if personalQB.objects.filter(TypeID=TypeID, questionID=questionID, statusID=statusID):
                return
            else:
                stuClass.totalCcompletionWrong = stuClass.totalCcompletionWrong + 1
    elif TypeID == SHORT_ANSWER:  # 简答
        if statusID == RIGHT:
            stuClass.totalShortAnswerRight = stuClass.totalShortAnswerRight + 1
        else:
            if personalQB.objects.filter(TypeID=TypeID, questionID=questionID, statusID=statusID):
                return
            else:
                stuClass.totalShortAnswerWrong = stuClass.totalShortAnswerWrong + 1
    stuClass.save()


@csrf_exempt
def stu_classJoin(request):  # 加入班级
    tch_account = request.POST.get('tch_account')  # 班级号
    stu_account = request.session.get('account')  # 学生账号
    try:
        Sel_Class = Class.objects.filter(tch_account=tch_account).values()[0]  # 查找班级
        peopleNum = Sel_Class.get('peopleNum', 0) + 1
        joined_stu_QB = personalQB.objects.filter(Account=stu_account)
        choiceRight = joined_stu_QB.filter(TypeID=1, statusID=1).count()
        choiceWrong = joined_stu_QB.count() - choiceRight
        completionRight = joined_stu_QB.filter(TypeID=2, statusID=1).count()
        completionWrong = joined_stu_QB.count() - completionRight
        shortAnswerRight = joined_stu_QB.filter(TypeID=3, statusID=1).count()
        shortAnswerWrong = joined_stu_QB.count() - shortAnswerRight
        choiceRight = Sel_Class.get('choiceRight', 0) + choiceRight
        choiceWrong = Sel_Class.get('choiceWrong', 0) + choiceWrong
        completionRight = Sel_Class.get('completionRight', 0) + completionRight
        completionWrong = Sel_Class.get('completionWrong', 0) + completionWrong
        shortAnswerRight = Sel_Class.get('shortAnswerRight', 0) + shortAnswerRight
        shortAnswerWrong = Sel_Class.get('shortAnswerWrong', 0) + shortAnswerWrong
        selfClass = Class(tch_account=tch_account, peopleNum=peopleNum,
                          totalChoiceRight=choiceRight, totalChoiceWrong=choiceWrong,
                          totalCompletionRight=completionRight, totalCcompletionWrong=completionWrong,
                          totalShortAnswerRight=shortAnswerRight, totalShortAnswerWrong=shortAnswerWrong)
        stu = Student.objects.filter(Account=stu_account).values()[0]
        name = stu.get('Name')
        gender = stu.get('Gender')
        age = stu.get('Age')
        psw = stu.get('Psw')
        email = stu.get('Email')
        motto = stu.get('Motto')
        Sel_Stu = Student(Name=name, Gender=gender, Age=age, Psw=psw, Email=email, Motto=motto, Account=stu_account,
                          Class=selfClass.tch_account)
        Sel_Stu.save()
        selfClass.save()
        Sel_Stu.save()
        return JsonResponse({'status': 200})
    except:
        return JsonResponse({'status': 500})
